package io.renren.modules.companys.service.impl;

import com.qiniu.util.Json;
import io.renren.modules.common.dao.AreasDao;
import io.renren.modules.common.dao.CitiesDao;
import io.renren.modules.common.dao.ProvincesDao;
import io.renren.modules.common.entity.AreasEntity;
import io.renren.modules.common.entity.CitiesEntity;
import io.renren.modules.common.entity.ProvincesEntity;
import io.renren.modules.common.viewdo.SelectOption;
import io.renren.modules.common.viewdo.TreeOption;
import io.renren.modules.companys.entity.dto.CompanyQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.companys.dao.CompanyDao;
import io.renren.modules.companys.entity.CompanyEntity;
import io.renren.modules.companys.service.CompanyService;


@Service("companyService")
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, CompanyEntity> implements CompanyService {


    @Autowired
    private ProvincesDao provincesDao;
    @Autowired
    private CitiesDao citiesDao;
    @Autowired
    private AreasDao areasDao;
    @Autowired
    private CompanyDao companyDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String jsonStr = (String) params.get("queryCondition");
        CompanyQueryDTO companyQueryDTO = Json.decode(jsonStr, CompanyQueryDTO.class);

        QueryWrapper<CompanyEntity> companyWrapper = new QueryWrapper<>();

        if (companyQueryDTO.getCompanyPropertyId()!=null){
            companyWrapper.eq("company_property_id",companyQueryDTO.getCompanyPropertyId());
        }

        if (companyQueryDTO.getProvincesId()!=null &&
        companyQueryDTO.getCityesId()!=null &&
        companyQueryDTO.getAreasId()!=null){
            companyWrapper.eq("provinces_id",companyQueryDTO.getProvincesId());
            companyWrapper.eq("cityes_id",companyQueryDTO.getCityesId());
            companyWrapper.eq("areas_id",companyQueryDTO.getAreasId());
        }

        if (companyQueryDTO.getTradeId()!=null &&
        companyQueryDTO.getClassifyId()!=null &&
        companyQueryDTO.getJobTypeId()!=null){
            companyWrapper.eq("trade_id",companyQueryDTO.getTradeId());
            companyWrapper.eq("classify_id",companyQueryDTO.getClassifyId());
            companyWrapper.eq("job_type_id",companyQueryDTO.getJobTypeId());
        }

        IPage<CompanyEntity> page = this.page(
                new Query<CompanyEntity>().getPage(params),
                companyWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<TreeOption> treeOPtionPCR() {
        List<TreeOption> topList = new ArrayList<>();

        List<ProvincesEntity> provincesEntities = provincesDao.selectList(null);
        List<CitiesEntity> citiesEntities = citiesDao.selectList(null);
        List<AreasEntity> areasEntities = areasDao.selectList(null);

        topList = provincesEntities.stream().map(item -> new TreeOption(item.getProvinceid(),item.getProvincename())).collect(Collectors.toList());

        for (TreeOption treeOption : topList){
            List<CitiesEntity> collect = citiesEntities.stream().filter(item -> treeOption.getValue().equals(item.getProvinceid())).collect(Collectors.toList());
            List<TreeOption> treeOptions = collect.stream().map(itme -> new TreeOption(itme.getCityid(), itme.getCityname())).collect(Collectors.toList());
            treeOption.setChildren(treeOptions);
        }

        for (TreeOption treeOption : topList){
            List<TreeOption> treeOptionsMid = treeOption.getChildren();
            for (TreeOption treeOptionMid : treeOptionsMid){
                List<AreasEntity> collect = areasEntities.stream().filter(item -> treeOptionMid.getValue().equals(item.getCityid())).collect(Collectors.toList());
                List<TreeOption> treeOptions = collect.stream().map(item -> new TreeOption(item.getAreaid(), item.getAreaname())).collect(Collectors.toList());
                treeOptionMid.setChildren(treeOptions);
            }
        }
        return topList;
    }

    @Override
    public List<SelectOption> selectOption() {
        List<CompanyEntity> companyEntities = companyDao.selectList(null);
        List<SelectOption> collect = companyEntities.stream().map(item -> new SelectOption(item.getId(), item.getCompanyName())).collect(Collectors.toList());
        return collect;
    }
}